﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ECS.Framework;

namespace E_ICS.E_ICS
{
    public partial class frmMAWBsearch : Form 
    {
        #region private properties
        private Main.frmMain2 main = null;
        DataSet _dsMAWB = new DataSet();
        private string _mawb = string.Empty;
        private string _mawbPrefix = string.Empty;
        private string _mawbSerial = string.Empty;
        private string _hawb = string.Empty;
        private string _flightNumber = string.Empty;
        private string _flightDate = string.Empty;
        private string _destination = string.Empty;
        private string _sequenceNumber = string.Empty;
        private string _agentCode = string.Empty;
        private string _agentName = string.Empty;
        private string _natureOfGoods = string.Empty;
        private string _shipmenType = string.Empty;
        private string _slipType = string.Empty;
        private string _origin = string.Empty;
        private string _selectHAWB = string.Empty;
        private string _selectFlightNumber = string.Empty;
        private string _selectFlightDate = string.Empty;
        private string _selectDestination = string.Empty;
        private string _selectSequenceNumber = string.Empty;
        private string _selectAgentCode = string.Empty;
        private string _selectAgentName = string.Empty;
        private string _selectNatureOfGoods = string.Empty;
        private string _selectShipmentType = string.Empty;
        private string _selectSlipType = string.Empty;
        private string _selectOrigin = string.Empty;
        private int _index = 0;
        #endregion
        #region form events
        public frmMAWBsearch(DataSet dataset, Main.frmMain2 _main)
        {
            InitializeComponent();
            _dsMAWB = dataset;
            main = _main;
        }
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            ClearValueSelectMAWB();
            ClearValueToAddMAWB();
            btnAdd.Enabled = false;
            btnSelect.Enabled = false;
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                
                int columnIndex = 0;
                _index = e.RowIndex;
                columnIndex = e.ColumnIndex;
                if (columnIndex == -1)
                {
                    columnIndex = 0;
                }
                _index = e.RowIndex;

                if (e.RowIndex >= 0)
                {
                    btnAdd.Enabled = true;
                    btnSelect.Enabled = true;
                    //dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dgvMAWB.Rows[_index].Selected = true;
                    if (dgvMAWB.Rows[_index].Cells[columnIndex].Value != null)
                    {
                        _selectHAWB = dgvMAWB.Rows[_index].Cells[1].Value.ToString();
                        _selectFlightNumber = dgvMAWB.Rows[_index].Cells[2].Value.ToString();
                        _selectFlightDate = dgvMAWB.Rows[e.RowIndex].Cells[3].Value.ToString();
                        _selectOrigin = dgvMAWB.Rows[_index].Cells[4].Value.ToString();
                        _selectDestination = dgvMAWB.Rows[_index].Cells[5].Value.ToString();
                        //SelSeq = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString();
                        _selectSequenceNumber = dgvMAWB.Rows.Count.ToString();
                        _selectAgentCode = dgvMAWB.Rows[_index].Cells[7].Value.ToString();
                        _selectAgentName = dgvMAWB.Rows[_index].Cells[8].Value.ToString();
                        _selectNatureOfGoods = dgvMAWB.Rows[_index].Cells[9].Value.ToString();

                        _selectShipmentType = dgvMAWB.Rows[_index].Cells[10].Value.ToString();
                        _selectSlipType = dgvMAWB.Rows[_index].Cells[11].Value.ToString();
                        InsertToArrayMain2(_index);

                        InsertToAddMAWB(_index);


                    }
                }
            }
            catch (Exception)
            {

                //
            }
        }
        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                if (_index == -1)
                {
                    InsertToAddMAWB(dgvMAWB.Rows.Count - 2);
                    InsertToArrayMain2(dgvMAWB.Rows.Count - 2);
                }
                string[] array = new string[11] { _selectHAWB, _selectFlightNumber, _selectFlightDate, _selectDestination, _selectSequenceNumber, _selectAgentCode, _selectAgentName, _selectNatureOfGoods, _selectShipmentType, _selectOrigin, _selectSlipType };
                main.getDataFromMAWBSearch(array);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch
            {

                //throw;
            }

        }
        private void btnMAWBAdd_Click(object sender, EventArgs e)
        {
            E_ICS.frmAddMAWB am1 = new E_ICS.frmAddMAWB(main);
            if (am1.ShowDialog() == DialogResult.OK)
            {
                this.DialogResult = DialogResult.OK;
                ClearValueToAddMAWB();
                this.Close();
            }


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ClearValueToAddMAWB();

            this.Dispose();
        }

        private void frmMAWBsearch_Load(object sender, EventArgs e)
        {
            DataTable dtMAWB = null;
            SetScreen();
            dtMAWB = CreateMAWBTable(_dsMAWB);
            //dataGridView1.DataSource = da.Tables[0];

            dgvMAWB.DataSource = dtMAWB;
            //dataGridView1.Columns[6].Visible = true;
            dgvMAWB.Columns[7].Visible = false;
            dgvMAWB.Columns[8].Visible = false;
            dgvMAWB.Columns[9].Visible = false;
            //dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[0].Selected = false;
            //dataGridView1.Rows[dataGridView1.Rows.Count-2].Selected = true;
            dgvMAWB.Rows[0].Cells[0].Selected = true;
            Selectdata();
            btnSelect.Focus();


        }
        #endregion
        #region private methods
        private void SetScreen()
        {
            this.Text = CommonResourceString.GetResourseValue("scn_MAWBSearch_frm_name");
            btnAdd.Text = CommonResourceString.GetResourseValue("scn_MAWBSearch_btnAdd");
            btnSelect.Text = CommonResourceString.GetResourseValue("scn_MAWBSearch_Select");
            btnCancel.Text = CommonResourceString.GetResourseValue("scn_MAWBSearch_Cancel");
        }
        /// <summary>
        /// create datatable for dataset
        /// </summary>
        /// <param name="ds">type DataSet</param>
        /// <returns></returns>
        private DataTable CreateMAWBTable(DataSet ds)
        {
            DataTable dtTW = new DataTable();
            ///////////create head column/////////////////
            dtTW.Columns.Add("MAWB", typeof(string));
            //TW.Columns.Add("Prefix", typeof(string));
            //TW.Columns.Add("Serial", typeof(string));
            dtTW.Columns.Add("HAWB", typeof(string));
            dtTW.Columns.Add("Flight No", typeof(string));
            dtTW.Columns.Add("Flight Date", typeof(string));
            dtTW.Columns.Add("Origination", typeof(string));
            dtTW.Columns.Add("Destination", typeof(string));
            dtTW.Columns.Add("Seq", typeof(string));
            dtTW.Columns.Add("Agent Code", typeof(string));
            dtTW.Columns.Add("Agent Name", typeof(string));
            dtTW.Columns.Add("Nature Of Good", typeof(string));
            dtTW.Columns.Add("Shipment Type", typeof(string));
            dtTW.Columns.Add("Slip Type", typeof(string));
            ///////////////////////////////////////////////
            int row = 0;
            row = ds.Tables[0].Rows.Count;
            for (int i = 0; i < row; i++)
            {
                _mawb = ds.Tables[0].Rows[i]["mawb_prefix"].ToString().Trim() + "-" + ds.Tables[0].Rows[i]["mawb_sn"].ToString().Trim();
                //Prefix = ds.Tables[0].Rows[i]["mawb_prefix"].ToString().Trim();
                //Serial = ds.Tables[0].Rows[i]["mawb_sn"].ToString().Trim();
                _hawb = ds.Tables[0].Rows[i]["hawb"].ToString().Trim();
                _flightNumber = ds.Tables[0].Rows[i]["display_flight_number"].ToString().Trim();
                _flightDate = ds.Tables[0].Rows[i]["display_flight_date"].ToString().Trim();
                _destination = ds.Tables[0].Rows[i]["destination"].ToString().Trim();
                _sequenceNumber = ds.Tables[0].Rows[i]["partial_seq"].ToString().Trim();
                _agentCode = ds.Tables[0].Rows[i]["agent_code"].ToString().Trim();
                _agentName = ds.Tables[0].Rows[i]["agent_name"].ToString().Trim();
                _natureOfGoods = ds.Tables[0].Rows[i]["nature_of_goods"].ToString().Trim();
                _shipmenType = ds.Tables[0].Rows[i]["shipment_type"].ToString().Trim();
                _origin = ds.Tables[0].Rows[i]["origination"].ToString().Trim();
                _slipType = ds.Tables[0].Rows[i]["slip_type"].ToString().Trim();
                dtTW.Rows.Add(_mawb, _hawb, _flightNumber, _flightDate, _origin, _destination, _sequenceNumber, _agentCode, _agentName, _natureOfGoods, _shipmenType, _slipType);
            }
            return dtTW;
        }
        /// <summary>
        /// Default selection for first row of datagridview when this form is loaded
        /// </summary>
        private void Selectdata()
        {
            //int index = dataGridView1.Rows.Count - 2;
            if (dgvMAWB.Rows[_index].Cells[0].Value != null)
            {
                _selectHAWB = dgvMAWB.Rows[_index].Cells[1].Value.ToString();
                _selectFlightNumber = dgvMAWB.Rows[_index].Cells[2].Value.ToString();
                _selectFlightDate = dgvMAWB.Rows[_index].Cells[3].Value.ToString();
                _selectOrigin = dgvMAWB.Rows[_index].Cells[4].Value.ToString();
                _selectDestination = dgvMAWB.Rows[_index].Cells[5].Value.ToString();
                //SelSeq = dataGridView1.Rows[index].Cells[6].Value.ToString();
                _selectSequenceNumber = dgvMAWB.Rows.Count.ToString();
                _selectAgentCode = dgvMAWB.Rows[_index].Cells[7].Value.ToString();
                _selectAgentName = dgvMAWB.Rows[_index].Cells[8].Value.ToString();
                _selectNatureOfGoods = dgvMAWB.Rows[_index].Cells[9].Value.ToString();
                _selectShipmentType = dgvMAWB.Rows[_index].Cells[10].Value.ToString();

                InsertToArrayMain2(_index);

                InsertToAddMAWB(_index);


            }
        }
        /// <summary>
        /// pass the data to form main2 when press select button
        /// </summary>
        /// <param name="p">_index</param>
        private void InsertToArrayMain2(int p)
        {
            _selectHAWB = dgvMAWB.Rows[p].Cells[1].Value.ToString();
            _selectFlightNumber = dgvMAWB.Rows[p].Cells[2].Value.ToString();
            _selectFlightDate = dgvMAWB.Rows[p].Cells[3].Value.ToString();
            _selectOrigin = dgvMAWB.Rows[_index].Cells[4].Value.ToString();
            _selectDestination = dgvMAWB.Rows[p].Cells[5].Value.ToString();
            //SelSeq = dataGridView1.Rows[p].Cells[5].Value.ToString();
            _selectSequenceNumber = dgvMAWB.Rows.Count.ToString();
            _selectAgentCode = dgvMAWB.Rows[p].Cells[7].Value.ToString();
            _selectAgentName = dgvMAWB.Rows[p].Cells[8].Value.ToString();
            _selectNatureOfGoods = dgvMAWB.Rows[p].Cells[9].Value.ToString();
            _selectShipmentType = dgvMAWB.Rows[p].Cells[10].Value.ToString();
            // throw new NotImplementedException();
        }
        /// <summary>
        /// pass the data to form addMAWB when press add button
        /// </summary>
        /// <param name="p">_index</param>
        private void InsertToAddMAWB(int p)
        {
            // ValueTofrmAddMAWB inserttoaddmawb = new ValueTofrmAddMAWB();

            ValueTofrmAddMAWB.SelPrefix = _dsMAWB.Tables[0].Rows[p]["mawb_prefix"].ToString().Trim();//dgvMAWB.Rows[p].Cells[0].Value.ToString().Substring(0,3);//
            ValueTofrmAddMAWB.SelSerial = _dsMAWB.Tables[0].Rows[p]["mawb_sn"].ToString().Trim(); //dgvMAWB.Rows[p].Cells[0].Value.ToString().Substring(4, 8);//
            ValueTofrmAddMAWB.Selhawb = dgvMAWB.Rows[p].Cells[1].Value.ToString();
            //  ValueTofrmAddMAWB.SelFlt_no = dataGridView1.Rows[p].Cells[2].Value.ToString();
            //ValueTofrmAddMAWB.SelFlt_no = da.Tables[0].Rows[p]["flight_number"].ToString().Trim();
            //ValueTofrmAddMAWB.SelFlt_date = dataGridView1.Rows[p].Cells[3].Value.ToString();
            ValueTofrmAddMAWB.Seldest = dgvMAWB.Rows[p].Cells[5].Value.ToString();
            ValueTofrmAddMAWB.SelOrigin = dgvMAWB.Rows[p].Cells[4].Value.ToString();
            //ValueTofrmAddMAWB.SelSeq = dataGridView1.Rows[p].Cells[5].Value.ToString();
            //ValueTofrmAddMAWB.SelAgentCode = dataGridView1.Rows[p].Cells[6].Value.ToString();
            //ValueTofrmAddMAWB.SelAgentName = dataGridView1.Rows[p].Cells[7].Value.ToString();
            //ValueTofrmAddMAWB.SelNatureOfGood = dataGridView1.Rows[p].Cells[8].Value.ToString();
            //ValueTofrmAddMAWB.Selshipment_type = dataGridView1.Rows[p].Cells[9].Value.ToString();
            //ValueTofrmAddMAWB.Selpiece_count = da.Tables[0].Rows[p]["piece_count"].ToString().Trim();
            //ValueTofrmAddMAWB.Selcarrier = da.Tables[0].Rows[p]["carrier"].ToString().Trim();
            //ValueTofrmAddMAWB.Selflight_date = da.Tables[0].Rows[p]["flight_date_std"].ToString().Trim();
        }
        public void ClearValueToAddMAWB()
        {
            ValueTofrmAddMAWB.SelPrefix = string.Empty;
            ValueTofrmAddMAWB.SelSerial = string.Empty;
            ValueTofrmAddMAWB.Selhawb = string.Empty;
            ValueTofrmAddMAWB.SelFlt_no = string.Empty;
            ValueTofrmAddMAWB.SelFlt_date = string.Empty;
            ValueTofrmAddMAWB.Seldest = string.Empty;
            ValueTofrmAddMAWB.SelSeq = string.Empty;
            ValueTofrmAddMAWB.SelAgentCode = string.Empty;
            ValueTofrmAddMAWB.SelAgentName = string.Empty;
            ValueTofrmAddMAWB.SelNatureOfGood = string.Empty;
            ValueTofrmAddMAWB.Selshipment_type = string.Empty;
            ValueTofrmAddMAWB.Selpiece_count = string.Empty;
            ValueTofrmAddMAWB.Selcarrier = string.Empty;
            ValueTofrmAddMAWB.Selflight_date = string.Empty;


        }
        private void ClearValueSelectMAWB()
        {
            dgvMAWB.ClearSelection();
            _selectFlightNumber = string.Empty;
            _selectFlightDate = string.Empty;
            _selectDestination = string.Empty;
            _selectSequenceNumber = string.Empty;
            _selectAgentCode = string.Empty;
            _selectAgentName = string.Empty;
            _selectNatureOfGoods = string.Empty;
            _selectShipmentType = string.Empty;
            _selectOrigin = string.Empty;

        }
        #endregion


        private void dgvMAWB_Sorted(object sender, EventArgs e)
        {
            btnAdd.Enabled = false;
            btnSelect.Enabled = false;
        }
    }
}
